package com.dgf.myboot.web;
import com.dgf.myboot.core.result.Result;
import com.dgf.myboot.core.result.ResultGenerator;
import com.dgf.myboot.model.SysRoleResource;
import com.dgf.myboot.service.SysRoleResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by dgf on 2017/07/18.
*/
@RestController
@RequestMapping("/sys/role/resource")
public class SysRoleResourceController {
    @Resource
    private SysRoleResourceService sysRoleResourceService;

    @PostMapping("/add")
    public Result add(SysRoleResource sysRoleResource) {
        sysRoleResourceService.save(sysRoleResource);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        sysRoleResourceService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(SysRoleResource sysRoleResource) {
        sysRoleResourceService.update(sysRoleResource);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        SysRoleResource sysRoleResource = sysRoleResourceService.findById(id);
        return ResultGenerator.genSuccessResult(sysRoleResource);
    }

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SysRoleResource> list = sysRoleResourceService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
